﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Waiter.View;
using Waiter.Common;
using System.Configuration;
using System.IO;

namespace Waiter
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            try
            {
                string currentSerialNumber = Waiter.Properties.Settings.Default.WAITER_SERIAL_NUMBER;
                string machineSerialNumber = CommonUtils.fsGetMachineSerialNumner();
                if (currentSerialNumber == CommonConstants.ENCODE_STRING)
                {
                    Waiter.Properties.Settings.Default.WAITER_SERIAL_NUMBER = machineSerialNumber;
                    Waiter.Properties.Settings.Default.Save();
                    currentSerialNumber = Waiter.Properties.Settings.Default.WAITER_SERIAL_NUMBER;

                    // change serialnumber
                    string fileName = @"Waiter.exe.config";
                    StreamReader r = new StreamReader(fileName);
                    string content = r.ReadToEnd();
                    content = content.Replace(CommonConstants.ENCODE_STRING, machineSerialNumber);
                    r.Close();

                    StreamWriter w = new StreamWriter(fileName, false);
                    w.Write(content);
                    w.Flush();
                    w.Close();
                }

                if (currentSerialNumber != machineSerialNumber)
                {
                    DevComponents.DotNetBar.MessageBoxEx.Show("Bạn không có quyền sử dụng phần mềm này!\nVui lòng liên hệ với nhà cung cấp để được hướng dẩn sử dụng!", CommonMessages.MSG_ERR_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new FrmMenu());
            }
            catch (Exception ex)
            {
                CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_ERROR, "Main", "", ex.ToString());
            }
            CommonLogging.fsDeleteUnUseLogFile();
        }
    }
}
